package com.shop.storage.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.shop.storage.domain.WmsForewarning;
import com.shop.storage.domain.WmsSupplier;
import com.shop.storage.service.IWmsForewarningService;
import com.shop.storage.service.IWmsSupplierService;
import com.shop.storage.vo.response.Requirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shop.storage.mapper.WmsPurchaseRequireMapper;
import com.shop.storage.domain.WmsPurchaseRequire;
import com.shop.storage.service.IWmsPurchaseRequireService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购清单Service业务层处理
 * 
 * @author shop
 * @date 2024-06-24
 */
@Service
public class WmsPurchaseRequireServiceImpl implements IWmsPurchaseRequireService 
{
    @Autowired
    private WmsPurchaseRequireMapper wmsPurchaseRequireMapper;

    @Autowired
    private IWmsSupplierService wmsSupplierService;
    @Autowired
    private IWmsForewarningService wmsForewarningService;

    /**
     * 查询采购清单
     * 
     * @param requireId 采购清单主键
     * @return 采购清单
     */
    @Override
    public WmsPurchaseRequire selectWmsPurchaseRequireByRequireId(Long requireId)
    {
        return wmsPurchaseRequireMapper.selectWmsPurchaseRequireByRequireId(requireId);
    }

    /**
     * 查询采购清单列表
     * 
     * @param wmsPurchaseRequire 采购清单
     * @return 采购清单
     */
    @Override
    public List<WmsPurchaseRequire> selectWmsPurchaseRequireList(WmsPurchaseRequire wmsPurchaseRequire)
    {
        return wmsPurchaseRequireMapper.selectWmsPurchaseRequireList(wmsPurchaseRequire);
    }

    /**
     * 新增采购清单
     * 
     * @param wmsPurchaseRequire 采购清单
     * @return 结果
     */
    @Override
    public int insertWmsPurchaseRequire(WmsPurchaseRequire wmsPurchaseRequire)
    {
        return wmsPurchaseRequireMapper.insertWmsPurchaseRequire(null);
    }

    /**
     * 修改采购清单
     * 
     * @param wmsPurchaseRequire 采购清单
     * @return 结果
     */
    @Override
    public int updateWmsPurchaseRequire(WmsPurchaseRequire wmsPurchaseRequire)
    {
        //远程调用查询单价
        BigDecimal a = new BigDecimal(20);
        //判断  是不是改变量
        if (wmsPurchaseRequire.getRequireSum()!=null){
            //查询拼接
           wmsPurchaseRequire.setRequireTotalPrices(a.multiply(BigDecimal.valueOf(wmsPurchaseRequire.getRequireSum())));
        }
        int i = wmsPurchaseRequireMapper.updateWmsPurchaseRequire(wmsPurchaseRequire);

        return i;
    }

    /**
     * 批量删除采购清单
     * 
     * @param requireIds 需要删除的采购清单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWmsPurchaseRequireByRequireIds(List<Long> requireIds)
    {
        //删除
        int i = wmsPurchaseRequireMapper.updateWmsPurchaseRequireWareSkuId(requireIds, 5L);
        //成功运行  未成功不处理
        if (i > 0){
            List<WmsForewarning> wmsForewarnings = new ArrayList<>();
            //循环
            for (Long requireId : requireIds) {
                WmsForewarning wmsForewarning = new WmsForewarning();
                wmsForewarning.setStatus(1L);
                wmsForewarning.setWareSkuId(requireId);
                wmsForewarnings.add(wmsForewarning);
            }
            //修改
            wmsForewarningService.updateWmsForewarningList(wmsForewarnings);
        }
        return i;
    }

    /**
     * 删除采购清单信息
     * 
     * @param requireId 采购清单主键
     * @return 结果
     */
    @Override
    public int deleteWmsPurchaseRequireByRequireId(Long requireId)
    {
        return wmsPurchaseRequireMapper.deleteWmsPurchaseRequireByRequireId(requireId);

    }

    //组合查询
    @Override
    public List<Requirement> selectWmsPurchaseRequireLists(String skuIds, String skuName) {
        if (skuIds.equals("0")){
            skuIds = "";
        }
        List<Integer> list = new ArrayList<>();
        if (skuIds.length() > 0){
            String[] split = skuIds.split(",");
            for (String s : split) {
                list.add(Integer.valueOf(s));
            }
        }
        //查询的数据
        List<Requirement> requirements = wmsPurchaseRequireMapper.selectWmsPurchaseRequireLists(list, skuName);
        return requirements;
    }

    //批量添加
    @Override
    @Transactional
    public int insertWmsPurchaseRequires(Long[] wareSkuIds) {
        int i = wmsPurchaseRequireMapper.insertWmsPurchaseRequires(wareSkuIds);
        if (i > 0){
            for (Long wareSkuId : wareSkuIds) {
                wmsForewarningService.updateWmsForewarningSkuId(wareSkuId,2L);
            }

        }
        return i;
    }

    @Override
    public void updateWmsPurchaseRequireWareSkuId(List<Long> id, Long status) {
        wmsPurchaseRequireMapper.updateWmsPurchaseRequireWareSkuId(id,status);
    }
}
